#include "vtor_modbus.h"


#ifdef __DEMO_PROJECT__



#define VtorModbus_RunTime				30001
#define VtorModbus_Key1Value			30002
#define VtorModbus_Key2Value			30003
#define VtorModbus_Key3Value			30004
#define VtorModbus_Key4Value			30005

#define VtorModbus_DevNumber			40001
#define VtorModbus_Led1State			40002
#define VtorModbus_Led2State			40003
#define VtorModbus_Led3State			40004
#define VtorModbus_Led4State			40005


VtorModbusRegister VtorModbus_demoRegisterArray[] = {
	//04指令 30000 只读
	30001, 3001,
	30002, 3002,
	30003, 3003,
	30004, 0,
	30005, 0,
	30006, 0,
	30007, 0,
	30008, 0,
	30009, 0,
	30010, 0,
	30011, 0,
	30012, 0,

	//03指令 40000地址 可读可写
	40001, 2025,
	40002, 4002,
	40003, 4003,
	40004, 4004,
	40005, 0,
	40006, 0,
	40007, 0,
	40008, 0,
	40009, 0,
	40010, 0,
	40011, 0,
	40012, 0,

	00000, 0
};

void VtorModbus_Init(uint8_t id)
{
	VtorModbus_id = id;
	VtorModbus_registerArray = VtorModbus_demoRegisterArray;
}

void VtorModbus_SendBuffer(uint8_t* buf, uint16_t len)
{
	//UART1_SendNByte(buf, len);
	//HAL_UART_Transmit(&huart1, buf, len, 0xFF);
}

void VtorModbusMaster_TestTask(void)
{
	// modbus主机测试代码
	// 往cmd03addr00写自增数据
	// cmd04addr[01-04] 被赋值成 cmd03addr[01-04]
	static uint16_t slaveReg[5] = {0};
	static uint16_t runCnt = 0;
	if(runCnt % 4 == 0)
	{
		// 读 可写0 到 slaveReg[0]
		VtorModbusMaster_ReadReg(1, VtorModbus_DevNumber, slaveReg, 1);
	}
	else if(runCnt % 4 == 1)
	{
		// 写 slaveReg[0] 到 可写1
		VtorModbusMaster_WriteReg(1, VtorModbus_Led1State, slaveReg, 1);
	}
	
	else if(runCnt % 4 == 2)
	{
		// 读 只读234 到 slaveReg234
		VtorModbusMaster_ReadReg(1, VtorModbus_Key2Value, &slaveReg[2], 3);
	}
	else if(runCnt % 4 == 3)
	{
		// 写 slaveReg234 到 可写234
		VtorModbusMaster_WriteReg(1, VtorModbus_Led2State, &slaveReg[2], 3);
	}
	runCnt++;
}

// 从机处理  主机写给从机的内存
void VtorModbusSlave_ProcessWriteCallback(uint16_t regAddr, uint16_t regNum)
{

}

// 主机处理 从机返回的内存
void VtorModbusMaster_ProcessReadCallback(uint16_t regAddr, uint16_t regNum)
{

}

void VtorModbusSlave_TestTask(void)
{
	// modbus从机测试代码
	// 只读0 自增
	// 只读1234 赋值到 可写1234
	static uint16_t slaveReg[4] = {0};
	static uint16_t runCnt = 0;
	VtorModbusSlave_WriteReg(VtorModbus_RunTime, &runCnt, 1);
	
	VtorModbusSlave_ReadReg(VtorModbus_Led1State, slaveReg, 4);
	VtorModbusSlave_WriteReg(VtorModbus_Key1Value, slaveReg, 4);
	runCnt++;
}

#endif
